import os
import requests
from PIL import Image
from io import BytesIO

os.makedirs('ppt_imgs', exist_ok=True)

img_urls = [
    # 1. 游戏行业销售演变
    'https://upload.wikimedia.org/wikipedia/commons/6/6a/Video_Game_Sales_Evolution.png',
    # 2. AI在游戏市场规模
    'https://upload.wikimedia.org/wikipedia/commons/2/2a/AI_in_Gaming_Market_Size_2022-2028.png',
    # 3. AI生成角色
    'https://upload.wikimedia.org/wikipedia/commons/2/2e/AI-generated-characters.png',
    # 4. AI NPC示例
    'https://upload.wikimedia.org/wikipedia/commons/7/7e/AI_NPC_example.png',
    # 5. AI反外挂流程图
    'https://upload.wikimedia.org/wikipedia/commons/3/3d/Game_anti-cheat_flowchart.png',
    # 6. AI自动化测试
    'https://upload.wikimedia.org/wikipedia/commons/4/4e/AI_Testing_Automation.png',
    # 7. AI推荐系统
    'https://upload.wikimedia.org/wikipedia/commons/5/5e/AI_Recommendation_System.png',
    # 8. AI+元宇宙
    'https://upload.wikimedia.org/wikipedia/commons/8/8e/Metaverse_and_AI.png',
    # 9. AI+云游戏
    'https://upload.wikimedia.org/wikipedia/commons/9/9e/Cloud_Gaming_AI.png',
    # 10. AI开发者工作场景
    'https://upload.wikimedia.org/wikipedia/commons/1/1e/AI_Developer_Working.png',
    # 11. AI在游戏行业的未来
    'https://upload.wikimedia.org/wikipedia/commons/0/0e/Future_of_AI_in_Gaming.png',
    # 12. 参考文献/致谢
    'https://upload.wikimedia.org/wikipedia/commons/3/3a/Academic_references_example.png',
]

for idx, url in enumerate(img_urls, 1):
    fname = f'ppt_imgs/{idx}.png'
    try:
        headers = {'User-Agent': 'Mozilla/5.0'}
        resp = requests.get(url, headers=headers, timeout=10)
        resp.raise_for_status()
        # 检查是否为图片
        try:
            img = Image.open(BytesIO(resp.content))
            img.verify()  # 检查图片完整性
            # 再次打开保存为png
            img = Image.open(BytesIO(resp.content))
            img.save(fname)
            print(f'图片{idx} 下载并验证成功: {fname}')
        except Exception as e:
            print(f'图片{idx} 下载失败或不是有效图片: {url} 错误: {e}')
            with open(fname, 'wb') as f:
                f.write(resp.content)
    except Exception as e:
        print(f'图片{idx} 下载失败: {url} 错误: {e}')

# 检查所有图片有效性
print('\n图片有效性检测:')
for idx in range(1, 13):
    fname = f'ppt_imgs/{idx}.png'
    try:
        img = Image.open(fname)
        img.verify()
        print(f'{fname} 有效')
    except Exception as e:
        print(f'{fname} 无效: {e}') 